//
//  HNSH_XJDRViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/5/30.
//

#import "HNSH_XJDRViewController.h"

//view
#import "HNSH_XJDRCell.h"
#import "HNSH_XJDROtherCell.h"
#import "HNSH_XJDRHeader.h"
#import "HNSH_XJDRFooter.h"
#import "HNSH_XJDRTopView.h"

#import "ST_PayPasswordView.h"

@interface HNSH_XJDRViewController ()

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;
/* headerView */
@property (nonatomic, strong) HNSH_XJDRTopView *headerView;
/* xjdrModel */
@property (nonatomic, strong) HNSH_XJDRModel *xjdrModel;

@end

static NSString *HNSH_XJDRCellID = @"HNSH_XJDRCellID";
static NSString *HNSH_XJDROtherCellID = @"HNSH_XJDROtherCellID";
static NSString *HNSH_XJDRHeaderID = @"HNSH_XJDRHeaderID";
static NSString *HNSH_XJDRFooterID = @"HNSH_XJDRFooterID";

@implementation HNSH_XJDRViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:@"星级达人" showLeftItem:YES];
    self.navView.backgroundColor = [UIColor clearColor];
    
    [self configTableView];
    
    [self loadFarmTeamData];
}

- (void)configTableView {
//    [self setupRefresh:self.tableView option:ST_RefreshDefault];
//    [self setupEmpty:self.tableView image:[UIImage imageNamed:@"empty_list_data"] title:@""];
    
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self.tableView registerClass:[HNSH_XJDRCell class] forCellReuseIdentifier:HNSH_XJDRCellID];
    [self.tableView registerClass:[HNSH_XJDROtherCell class] forCellReuseIdentifier:HNSH_XJDROtherCellID];
    [self.tableView registerClass:[HNSH_XJDRHeader class] forHeaderFooterViewReuseIdentifier:HNSH_XJDRHeaderID];
    [self.tableView registerClass:[HNSH_XJDRFooter class] forHeaderFooterViewReuseIdentifier:HNSH_XJDRFooterID];
    self.tableView.backgroundColor = [UIColor appWhiteLabelColor];
    
    self.headerView = [[HNSH_XJDRTopView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight+144)];
    self.tableView.tableHeaderView = self.headerView;
}

#pragma mark - UITableViewDataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.xjdrModel.list.count;
    }
    return self.xjdrModel.other_list.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 50.01f;
    }
    return 60.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return 48.0f;
    }
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 1) {
        HNSH_XJDRFooter *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HNSH_XJDRFooterID];
        footer.titleLabel.text = self.xjdrModel.other_desc;
        return footer;
    }
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HNSH_XJDRHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HNSH_XJDRHeaderID];
    if (section == 0) {
        header.iconImgV.image = [UIImage imageNamed:@"xjdr_cell_header_icon"];
        header.titleLabel.text = [NSString stringWithFormat:@"%@%@",self.xjdrModel.title,self.xjdrModel.subtitle];
    } else {
        header.iconImgV.image = [UIImage imageNamed:@"xjdr_cell_header_icon_other"];
        header.titleLabel.text = self.xjdrModel.other_title;
    }
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 64;
    }
    return 72;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HNSH_XJDRCell * cell = [tableView dequeueReusableCellWithIdentifier:HNSH_XJDRCellID forIndexPath:indexPath];
        cell.model = self.xjdrModel.list[indexPath.row];
        return cell;
    } else {
        HNSH_XJDROtherCell * cell = [tableView dequeueReusableCellWithIdentifier:HNSH_XJDROtherCellID forIndexPath:indexPath];
        cell.model = self.xjdrModel.other_list[indexPath.row];
        WeakSelf(weakSelf);
        cell.buttonBlock = ^(HNSH_XJDROtherItemModel * _Nonnull selectModel) {
            [weakSelf payPartnerBonusActivateWithModel:selectModel];
        };
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

#pragma mark networking
- (void)loadFarmTeamData {
    [HNSH_UserNetServer requestPartnerBonusInfoSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            self.xjdrModel = [HNSH_XJDRModel yy_modelWithDictionary:responseObject.resultset];
            self.headerView.model = self.xjdrModel;
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {

    }];
    
}

//MARK: 支付
- (void)payPartnerBonusActivateWithModel:(HNSH_XJDROtherItemModel *)model {
    WeakSelf(weakSelf);
    [ST_PayPasswordView showPasswordViewWithSureBlock:^(NSString * _Nonnull password) {
        [weakSelf loadPartnerBonusActivateWithModel:model pwd:password];
    } cancelBlock:^{
        
    }];
}

- (void)loadPartnerBonusActivateWithModel:(HNSH_XJDROtherItemModel *)model pwd:(NSString *)pwd {
    [HNSH_UserNetServer requestPartnerBonusActivateWithLevel:model.level pay_password:pwd success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.view makeToast:@"激活成功"];
            [self loadFarmTeamData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}


@end
